package com.wlr.blog.web.common.utils;

import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author lrwang
 * @version 1.0
 * @date 2022/11/16 10:30
 */
@Data
public class PageResult implements Serializable {
    /**
     * 当前页数
     */
    private int pageNum;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总记录数
     */
    private int totalCount;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 列表数据
     */
    private List<?> list;

    public PageResult(List<?> list, int totalCount, int pageSize, int pageNum) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.pageNum = pageNum;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }

    public PageResult(List<?> list) {
        this.list = list;
        this.totalCount = (int) new PageInfo(list).getTotal();
        try {
            this.pageSize = ServletUtils.getParameterToInt(PageUtil.PAGE_SIZE);
            this.pageNum = ServletUtils.getParameterToInt(PageUtil.PAGE_NUM);
        } catch (NullPointerException e) {
            this.pageSize = 10;
            this.pageNum = 1;
        }

        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }

    public PageResult(List<?> list, int totalCount) {
        this.list = list;
        this.totalCount = totalCount;
        try {
            this.pageSize = ServletUtils.getParameterToInt(PageUtil.PAGE_SIZE);
            this.pageNum = ServletUtils.getParameterToInt(PageUtil.PAGE_NUM);
        } catch (NullPointerException e) {
            this.pageSize = 10;
            this.pageNum = 1;
        }
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }
}
